package org.jash.imdemo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

public class MainActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        findViewById(R.id.register).setOnClickListener(v -> {
            new Thread(() -> {
                try {
                    EMClient.getInstance().createAccount(username.getText().toString(), password.getText().toString());
                    runOnUiThread(() -> {
                        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(this, LoginActivity.class).putExtra("username", username.getText().toString()).putExtra("password", password.getText().toString()));
                        finish();
                    });
                } catch (HyphenateException e) {
                    Log.e("TAG", "onCreate: " + e.getErrorCode() + ", " + e.getDescription(), e);
                    runOnUiThread(() -> Toast.makeText(this, "注册失败, 原因: " + e.getDescription(), Toast.LENGTH_SHORT).show());
                }
            }).start();
        });
        findViewById(R.id.go_login).setOnClickListener(v -> {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        });
        if (EMClient.getInstance().isLoggedInBefore()) {
            startActivity(new Intent(this, WechatActivity.class));
            finish();
        }
    }
}